package cn.itcast.controller;

import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.pojo.Role;
import cn.itcast.service.PermissionServive;
import cn.itcast.service.RoleMenuService;
import cn.itcast.service.RoleService;
import cn.itcast.entity.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import cn.itcast.constant.MessageConstant;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping(path = "/role")
@CrossOrigin
public class RoleController {


    @Reference
    private RoleService roleService;

    @Reference
    private PermissionServive permissionServive;

    @Reference
    private RoleMenuService roleMenuService;


    //分页查询角色
    @GetMapping(path = "/page")
    public Result findPage(QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = roleService.pageQuery(queryPageBean);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_ROLE_FAIL);
        }

    }




    //新增角色
    @PostMapping
    public Result add(@RequestBody Role role,String[] permissionIds,String[]  menuIds ) {
        try {
            roleService.add(role,permissionIds,menuIds);

            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }


    //根据id删除角色
    @DeleteMapping(path = "/{id}")
    public Result deleteById(@PathVariable Integer id) {
        try {
            //删除检查项
            roleService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (RuntimeException e) {
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL1);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    //根据id查询数据详情
    @GetMapping(path = "/{id}")
    public Result findById(@PathVariable int id) {
        try {
            //查询检查项
            Role role = roleService.findById(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //编辑角色
    @PutMapping("/edit")
    public Result edit(@RequestBody Role role) {
        try {
            roleService.edit(role);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    //保存和修改角色关联菜单
    @PutMapping
    public Result saveAll(String permissionIds, String menuIds) {
        try {
            String[] split = menuIds.split(",");
            //批量修改
            roleMenuService.saveAll(permissionIds, split);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }


    //通过id查询角色关联的菜单
    //axios.get("/role/" + row.id + "/menu")
    @GetMapping("/{id}/menu")
    public Result menu(@PathVariable("id") Integer id) {
        try {
            String[] ids = roleService.findByMenuId(id);

            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, ids);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }


    //查询角色关联的权限信息
    //axios.get("/role/" + row.id + "/permission")
    @GetMapping("/{id}/permission")
    public Result permission(@PathVariable("id") Integer id) {
        try {
            String[] ids = roleService.findByIdPermission(id);

            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, ids);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }

    }

//    /*查询全部权限*/
//    @GetMapping("/all")
//    public Result roleall(){
//        Result Result= roleService.roleall();
//        return Result;
//    }


}
